
<template>
<div>

  <el-card>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>课程负责人</el-breadcrumb-item>
      <el-breadcrumb-item>课程信息管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程详细内容信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="searchForm">

      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="queryInfo.query" placeholder="请输入章节顺序" clearable @clear="getCourseList"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="" @click="getCourseList" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-button style="position: absolute; right: 720px" type="" @click="addDialogVisible=true">添加章节</el-button>
        <el-button style="position: absolute; right: 100px" @click="exports">导出PDF</el-button>
      </el-form>

      <el-dialog title="添加章节" :visible.sync="addDialogVisible" width="40%" @close="addDialogClose">
        <el-form :model="addForm" :rules="FormRules" ref="addFormRef" label-width="100px">

          <el-form-item label="课程编号" prop="courseid">{{this.$session.get('bulls')}}
            <el-input v-model="addForm.courseid" v-show="false" readonly="readonly"></el-input>
          </el-form-item>


          <el-form-item label="顺序" prop="ord">
            <el-select v-model="addForm.ord">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
              <el-option label="4" value="4"></el-option>
              <el-option label="5" value="5"></el-option>
              <el-option label="6" value="6"></el-option>
              <el-option label="7" value="7"></el-option>
              <el-option label="8" value="8"></el-option>
              <el-option label="9" value="9"></el-option>
              <el-option label="10" value="10"></el-option>
              <el-option label="11" value="11"></el-option>
              <el-option label="12" value="12"></el-option>
              <el-option label="13" value="13"></el-option>
              <el-option label="14" value="14"></el-option>
              <el-option label="15" value="15"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="章节" prop="username" >
            <el-input type="textarea" v-model="addForm.coursedata" ></el-input>
          </el-form-item>



          <el-form-item label="时长" prop="attribute">
            <el-select v-model="addForm.coursetime">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目标" prop="attribute">
            <el-input type="textarea" v-model="addForm.goal"></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCourseParagraph">确 认</el-button>
        </div>
      </el-dialog>





      <div id = "pdfDom">
      <el-table :header-cell-style="{background:'#808080',color:'#FFFFFF'}" border stripe :data="List" style="width: 100%">

        <el-table-column prop="ord" label="顺序" width="80" align="center">
        </el-table-column>
        <el-table-column prop="coursedata" label="章节内容" width="500" align="center">
        </el-table-column>
        <el-table-column prop="coursetime" label="时长" width="80" align="center">
        </el-table-column>
        <el-table-column prop="goal" label="目标" align="center">
        </el-table-column>

        <el-table-column label="修改  /  删除" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑章节" placement="top-start">
              <el-button  icon="el-icon-edit" circle @click="editParagraph(scope.row.courseid,scope.row.ord)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除章节" placement="top-start">
              <el-button type="" icon="el-icon-delete" circle @click="deleteParagraph(scope.row.courseid,scope.row.ord)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>


      </el-table>
      </div>

      <el-dialog title="修改课程信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose" append-to-body = "true">
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">


          <el-form-item label="章节" prop="coursedata">
            <el-input type="textarea" v-model="editForm.coursedata" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="时长" prop="coursetime">
            <el-select v-model="editForm.coursetime" autocomplete="off">
              <el-option label="1" value="1"></el-option>
              <el-option label="2" value="2"></el-option>
              <el-option label="3" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目标" prop="goal">
            <el-input type="textarea" v-model="editForm.goal" autocomplete="off"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editParagraphInfo">修 改</el-button>
        </div>
      </el-dialog>

    </div>

    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[4]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </el-card>
</div>
</template>


<script>
  export default {

    created(){
      this.getCourseList();
    },
    data() {
      return {
        fileName: this.$session.get('coursename') + '课程详细内容信息',
        queryInfo:{
          query:'',//查询信息
          role:this.$session.get('bulls'),
          pageNum:1,
          pageSize:4
        },
        List:[],
        total:0,
        addDialogVisible:false,
        editDialogVisible:false,
        infoDialogVisible:false,
        Form:{
          courseid:'',
          ord:'',
          coursedata:'',
          coursetime:'',
          goal:'',

        },
        addForm:{
          courseid:this.$session.get('bulls'),
          ord:'',
          coursedata:'',
          coursetime:'',
          goal:'',

        },
        editForm:{
          coursedata:'',
          coursetime:'',
          goal:'',
        },
        editFormRules: {
          coursedata: [
            { required: true, message: '请输入章节信息', trigger: 'blur' },
          ],
          coursetime: [
            { required: true, message: '请输入时长', trigger: 'blur' },

          ],
          goal: [
            { required: true, message: '请输入章节目标', trigger: 'blur' },
          ],

        },
        addFormRules: {
          courseid: [
            { required: true, message: '请输入课程编号', trigger: 'blur' },
          ],
          ord: [
            { required: true, message: '请输入顺序', trigger: 'blur' },

          ],
          coursedata: [
            { required: true, message: '请输入章节信息', trigger: 'blur' },
          ],

          coursetime: [
            { required: true, message: '请输入时长', trigger: 'blur' },

          ],
          goal: [
            { required: true, message: '请输入章节目标', trigger: 'blur' },

          ],

        },
        FormRules: {

        },

      }
    },


    methods: {
      exports () {
			    this.getPdf('pdfDom', this.fileName)
		  },
      async getCourseList () {
        const { data: res } = await this.$http.get("CoursePages/AllCoursePage" ,{ params: this.queryInfo });
        this.List = res.data;
        this.total = res.numbers;
      },
      editParagraphInfo () {
        this.$refs.editFormRef.validate(async valid => {
          console.log(valid);
          if (!valid) return;

          const { data: res } = await this.$http.put("CoursePages/CoursePage", this.editForm);
          console.log(res);
          if (res !== "success") return this.$message.error("修改失败");
          this.$message.success("修改成功");

          this.editDialogVisible = false;
          this.getCourseList();
        });
      },
      async editParagraph(courseid,ord){
        const {data:res} = await this.$http.get("CoursePages/CoursePageMessage?courseid="+courseid+"&ord="+ord+"");
        this.editForm = res;
        this.editDialogVisible = true;
      },
      handleSizeChange (newSize) {
        this.queryInfo.pageSize = newSize;
        this.getCourseList();
      },
      handleCurrentChange (newPage) {
        this.queryInfo.pageNum = newPage;
        this.getCourseList();
      },
      addDialogClose () {
        this.$refs.addFormRef.resetFields();
      },
      editDialogClose () {
        this.$refs.editFormRef.resetFields();
      },
      ret(){
        this.$router.push({ path: "../manageCourse"});
      },

      async deleteParagraph (courseid,ord) {

        const confirmResult = await this.$confirm('是否删除该章节？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult !== 'confirm') {
          return ;
        }
        const { data: res } = await this.$http.delete("CoursePages/CoursePage?courseid="+ courseid+"&ord="+ord+"");
        if (res !== "success") {
          return this.$message.error("删除失败");
        }
        this.$message.success("删除成功");
        this.getCourseList();
      },
      addCourseParagraph () {
        for(var i = 0;i < this.List.length;i++){
          if(this.addForm.ord == this.List[i].ord){
            return this.$message.error("请不要添加重复顺序!")
          }
        }
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) return;
          const { data: res } = await this.$http.post("CoursePages/CoursePage", this.addForm);
          if (res !== "success") {
            return this.$message.error("添加失败!")
          }
          this.$message.success("添加成功！")
          this.addDialogVisible = false;
          this.getCourseList();
        })
      },
    }
  }
</script>




<style scoped>
  .el-header{
    background: lightslategray;
    color: white;
    font-size: 25px;
    align-items: center;
    justify-content: space-between;
    display: flex;
  }
  .el-main{
    background-color: whitesmoke;
  }

</style>
